package com.lyy.msgtool.controller;

import com.lyy.msgtool.config.RabbitTypeConfig;
import com.lyy.msgtool.controller.param.VehicleParam;
import com.lyy.msgtool.controller.vo.BaseResult;
import com.lyy.msgtool.controller.vo.RelateVehicleVo;
import com.lyy.msgtool.util.DataGeneration;
import com.lyy.msgtool.util.ExcelUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.jms.Queue;
import javax.jms.Topic;
import java.util.List;
import java.util.stream.Collectors;

@RestController("/msg-tool")
public class ToolController {
    @Resource
    private KafkaTemplate<String,Object> kafkaTemplate;
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private RabbitTypeConfig rabbitTypeConfig;
    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Resource
    private Queue amqQueue;
    @Resource
    private Topic amqTopic;

    @Value("${spring.kafka.topic.kafkaTopic:}")
    private String topic;
    @Value("${spring.jms.pub-sub-domain:false}")
    private boolean isAmqTopicType;

    @PostMapping("/message/sendKafka")
    public String sendKafka(@RequestParam("file") MultipartFile file){
        if(file.isEmpty()){
            return "文件不存在";
        }
        List<String> msgs = ExcelUtils.parseExcelToList(file, 0);
        for(String msg:msgs) {
            kafkaTemplate.send(topic, msg);
        }
        return "success";
    }

    @PostMapping("/message/sendRabbitmq")
    public String sendRabbitmq(@RequestParam("file") MultipartFile file){
        if(file.isEmpty()){
            return "文件不存在";
        }
        List<String> msgs = ExcelUtils.parseExcelToList(file, 0);
        for(String msg:msgs) {
            rabbitTemplate.convertAndSend(rabbitTypeConfig.getExchange(),rabbitTypeConfig.getRouteKey(),msg);
        }
        return "success";
    }

    @PostMapping("/message/sendAmq")
    public String sendAmq(@RequestParam("file") MultipartFile file){
        if(file.isEmpty()){
            return "文件不存在";
        }
        List<String> msgs = ExcelUtils.parseExcelToList(file, 0);
        for(String msg:msgs) {
            if(!isAmqTopicType){
                jmsMessagingTemplate.convertAndSend(amqQueue,msg);
            }else {
                jmsMessagingTemplate.convertAndSend(amqTopic,msg);
            }

        }
        return "success";
    }

    @PostMapping("/queryVehicle")
    public BaseResult<List<RelateVehicleVo>> queryVehicle(@RequestBody VehicleParam vehicleParam){
        List<RelateVehicleVo> relateVehicleVos = DataGeneration.vehicleData();
        List<RelateVehicleVo> collect = relateVehicleVos.stream().filter(v -> v.getPlateNo().equals(vehicleParam.getPlateNo())
                && v.getPlateType().equals(vehicleParam.getPlateType())).collect(Collectors.toList());
        return BaseResult.success(collect);
    }

    @PostMapping("/queryUntreatedViolationCount")
    public BaseResult<Integer> queryUntreatedViolationCount(@RequestBody VehicleParam vehicleParam){
        List<RelateVehicleVo> relateVehicleVos = DataGeneration.vehicleData();
        List<RelateVehicleVo> collect = relateVehicleVos.stream().filter(v -> v.getPlateNo().equals(vehicleParam.getPlateNo())
                && v.getPlateType().equals(vehicleParam.getPlateType())).collect(Collectors.toList());
        return BaseResult.success(collect.get(0).getUntreatedViolationCount());
    }


}
